import pygame
import random
import sys

# 初始化 Pygame 库
pygame.init()

# 设置窗口尺寸和格子尺寸
cell_size = 30
board_size = 10
window_size = (cell_size * board_size, cell_size * board_size)

# 定义颜色
black = (0, 0, 0)
white = (255, 255, 255)
gray = (128, 128, 128)
red = (255, 0, 0)
green = (0, 255, 0)

# 创建窗口
window = pygame.display.set_mode(window_size)
pygame.display.set_caption('Minesweeper')

# 创建字体
font = pygame.font.SysFont(None, 48)

# 生成雷
mine_count = 10
mine_locations = random.sample(range(board_size * board_size), mine_count)
board = [[0 for _ in range(board_size)] for _ in range(board_size)]
for mine_location in mine_locations:
    row = mine_location // board_size
    col = mine_location % board_size
    board[row][col] = -1

# 计算周围雷数
for i in range(board_size):
    for j in range(board_size):
        if board[i][j] == -1:
            continue
        count = 0
        for k in range(i-1, i+2):
            for l in range(j-1, j+2):
                if k >= 0 and k < board_size and l >= 0 and l < board_size and board[k][l] == -1:
                    count += 1
        board[i][j] = count

# 检查游戏是否结束
def check_game():
    for i in range(board_size):
        for j in range(board_size):
            if board[i][j] != -1 and not open[i][j]:
                return False
    return True

# 打开格子
def open_cell(x, y):
    if board[x][y] == -1:
        return False
    open[x][y] = True
    return True

# 标记雷
def flag_cell(x, y):
    flagged[x][y] = not flagged[x][y]

# 显示游戏面板和雷
def show_board():
    window.fill(white)
    for i in range(board_size):
        for j in range(board_size):
            rect = pygame.Rect(j * cell_size, i * cell_size, cell_size, cell_size)
            pygame.draw.rect(window, gray, rect, 1)
            if open[i][j]:
                if board[i][j] == -1:
                    pygame.draw.circle(window, red, rect.center, cell_size // 2)
                else:
                    text = font.render(str(board[i][j]), True, black)
                    text_rect = text.get_rect(center=rect.center)
                    window.blit(text, text_rect)
            elif flagged[i][j]:
                text = font.render('F', True, black)
                text_rect = text.get_rect(center=rect.center)
                window.blit(text, text_rect)

# 初始化 open 和 flagged 数组
open = [[False for _ in range(board_size)] for _ in range(board_size)]
flagged = [[False for _ in range(board_size)] for _ in range(board_size)]

# 游戏循环
game_over = False
while not game_over:

    # 每次循环检查游戏是否结束
    game_over = check_game()
    if game_over:
        # 显示游戏胜利画面
        text = font.render('You Win!', True, green)
        text_rect = text.get_rect(center=(window_size[0] // 2, window_size[1] // 2))
        window.fill(white)
        window.blit(text, text_rect)
        pygame.display.update()
        pygame.time.delay(3000)
        break

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            row = y // cell_size
            col = x // cell_size
            if event.button == 1:
                if not open[row][col]:
                    if not open_cell(row, col):
                        # 显示游戏失败画面
                        text = font.render('You Lose!', True, red)
                        text_rect = text.get_rect(center=(window_size[0] // 2, window_size[1] // 2))
                        window.fill(white)
                        window.blit(text, text_rect)
                        pygame.display.update()
                        pygame.time.delay(3000)
                        sys.exit()
            elif event.button == 3:
                flag_cell(row, col)

    # 显示游戏面板和雷
    show_board()

    # 更新显示
    pygame.display.update()

    # 退出 Pygame 库
pygame.quit()

